﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.ORM.DOM;
using System.ComponentModel;

namespace XDesigner.ORM
{
    [Serializable()]
    [System.ComponentModel.TypeConverter( typeof( ORMConfigTypeConverter ))]
    public class ORMConfig : ICloneable
    {
        public ORMConfig()
        {
        }

        private ORMBufferLevel _BufferLevel = ORMBufferLevel.Specify;
        [DefaultValue( ORMBufferLevel.Specify )]
        public ORMBufferLevel BufferLevel
        {
            get { return _BufferLevel; }
            set { _BufferLevel = value; }
        }


        private ParameterStyle _ParameterStyle = ParameterStyle.SQLServerStyle;
        /// <summary>
        /// Parameter style in sql command text
        /// </summary>
        [System.ComponentModel.DefaultValue(ParameterStyle.SQLServerStyle)]
        public ParameterStyle ParameterStyle
        {
            get
            {
                return _ParameterStyle;
            }
            set
            {
                _ParameterStyle = value;
            }
        }


        private bool _DebugMode = false;
        /// <summary>
        /// Whether framework in debug mode,=
        /// </summary>
        [System.ComponentModel.DefaultValue(false)]
        public bool DebugMode
        {
            get { return _DebugMode; }
            set { _DebugMode = value; }
        }

        private bool _EnableDelayLoad = true;
        /// <summary>
        /// Enable delay load ability.
        /// </summary>
        [System.ComponentModel.DefaultValue( true )]
        public bool EnableDelayLoad
        {
            get
            {
                return _EnableDelayLoad; 
            }
            set
            {
                _EnableDelayLoad = value; 
            }
        }

        private bool _ThrowDynamicCompilerError = true;
        /// <summary>
        /// Throw exception when happen dynamic compiler error.
        /// </summary>
        [System.ComponentModel.DefaultValue( true )]
        public bool ThrowDynamicCompilerError
        {
            get
            {
                return _ThrowDynamicCompilerError; 
            }
            set
            {
                _ThrowDynamicCompilerError = value; 
            }
        }

        private bool _UpdateImmediately = false;
        /// <summary>
        /// When user modify instance property value or delete instance, 
        /// update database immediately.
        /// </summary>
        [System.ComponentModel.DefaultValue( false )]
        public bool UpdateImmediately
        {
            get
            {
                return _UpdateImmediately; 
            }
            set
            {
                _UpdateImmediately = value; 
            }
        }

        object ICloneable.Clone()
        {
            ORMConfig cfg = new ORMConfig();
            cfg._EnableDelayLoad = this._EnableDelayLoad;
            cfg._BufferLevel = this._BufferLevel;
            cfg._ParameterStyle = this._ParameterStyle;
            cfg._DebugMode = this._DebugMode;
            cfg._ThrowDynamicCompilerError = this._ThrowDynamicCompilerError;
            cfg._UpdateImmediately = this._UpdateImmediately;
            return cfg;
        }

        public ORMConfig Clone()
        {
            return (ORMConfig)((ICloneable)this).Clone();
        }
    }

    public class ORMConfigTypeConverter : TypeConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(value, attributes);
        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
